<?php
/**
 * Created by PhpStorm.
 * User: zengyuming
 * Date: 2017/8/12
 * Time: 下午5:00
 */

namespace Admin\Controller;
class MerchantController extends CommonController{
    public function Index(){
        $ModelClass = new \Admin\Model\merchantModel();
        $Data = $ModelClass->getMerchantInfo();
        $this->assign('userlist' , $Data);
        $this->assign('CountSum' , count($Data));
        $this->assign('current_Titlename' , '商户管理');
        $this->display('Merchant_list');
    }

    public function mchAdd(){
        if(IS_POST){
            $this->_mchAdd();
            exit;
        }
        $mch_id = M("user")->field('merchant_id')->order('id desc')->find()['merchant_id'];
        $this->assign('current_Titlename' , '商户添加');
        $this->assign('mch_id' , intval($mch_id + 1)); // 商户号递增
        $this->assign('mch_key',md5(randNumber('md5')));

        $this->display('Merchant_add');
    }

    public function _mchAdd(){
        $data = I("post.");
        if($data['beizhu'] == '' || $data['sername'] == '' || strlen($data['mch_id']) > 5) exit('缺少必填参数或参数不合法'); // 一句话验证

        if(M("user")->where(array('merchant_id' => $data['mch_id']))->count() > 0 || M("merchant")->where(array('merchant_id' => $data['mch_id']))->count() > 0){
            $this->error('未知错误');
            exit;
        }

        $add = array(
            'username' => $data['sername'],
            'merchant_id' => $data['mch_id'],
            'password' => md5(randNumber('md5')),
            'Userremarks' => $data['beizhu'],
            'merchant_phone' => $data['phone'],
        );
        $Userid = M("user")->add($add);

        $mcerchData = array(
            'merchant_id' => $data['mch_id'],
            'merchant_key' => $data['mch_key'],
            'userid' => $Userid,
        );
        $addstatus = M("merchant")->add($mcerchData);

        if($Userid && $addstatus) {
            $this->success('添加' . $data['mch_id'] . '商户号成功');
        }else{
            $this->error('添加失败');
        }
    }

}